<?php
/******************************************************************************
 *
 *    OpenZoowa - Web App to manage events
 *    Copyright (C) 2008 Jesus Ruiz-Ayucar
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/
   if (!defined('ZOOWA')) {
      include_once('../../config.php');
   }
   require_once(modelspath . 'region.php');

   class City {
      private $id;
      private $region;
      private $name;
      private $name_clean;

      public function City($id = null) {
         if (isset($id) && Util::is_numeric($id) && $id > 0) {
            global $db;
            $SQL = 'SELECT * FROM '.$GLOBALS["db_pre"].'cities ';
            $SQL .= 'WHERE id = ' . $db->escape($id);
            $city = $db->get_row($SQL);
            $this->id = $city->id;
            $this->name = $city->name;
            $this->name_clean = $city->name_clean;
            $this->region = new Region($city->region_id);
         }
      }

      public function setName($name) {
         if (!empty($name) && strlen($name) <= 128) {
            $this->name = $name;
         }
      }

      public function setNameClean($name_clean) {
         if (!empty($name_clean) && strlen($name_clean) <= 128) {
            $this->name_clean = $name_clean;
         }
      }

      public function setRegion($region_id) {
         $region = new Region($region_id);
         $region_id = $region->getId();
         if (Util::is_numeric($region_id) && isset($region) && isset($region_id)) {
            $this->region = $region;
         } else {
            $this->region = null;
         }
      }

      public function add() {
         if (!isset($this->id) && !empty($this->name) && !empty($this->name_clean) && isset($this->region)) {
            global $db;
            $SQL  = 'INSERT INTO '.$GLOBALS["db_pre"].'cities ';
            $SQL .= '(name, name_clean, region_id) VALUES (';
            $SQL .= '"'.$db->escape($this->name).'",';
            $SQL .= '"'.$db->escape($this->name_clean).'",';
            $SQL .= $db->escape($this->region->getId()).')';
            $db->query($SQL);
            $this->id = $db->insert_id;
            return $this->id;
         }
      }

      public function del() {
         if (isset($this->id)) {
            global $db;

            $SQL  = 'DELETE FROM '.$GLOBALS["db_pre"].'cities ';
            $SQL .= 'WHERE id = '.$db->escape($this->id);
            if($db->query($SQL)) {
               $this->name = null;
               $this->name_clean = null;
               $this->region = null;
               $this->id = null;
            }
         }
      }

      public function getId() {
         return $this->id;
      }

      public function getRegion() {
         return $this->region;
      }

      public function getName() {
         return $this->name;
      }

      public function getNameClean() {
         return $this->name_clean;
      }

      public function __toString() {
         return $this->name.', '.$this->region->getName();
      }

/**
*	HELPERS
*/
      public static function getList($region) {
         global $db;

         if(isset($region)) {
            $SQL  = 'SELECT id FROM '.$GLOBALS['db_pre'].'cities WHERE ';
            $SQL .= 'region_id = '.$region;
            foreach($db->get_col($SQL) as $city) {
               $cities[] = new City($city);
            }
            return $cities;
         } else {
            return null;
         }
      }

      public static function getIdFromClean($city_name, $reg_id) {
         global $db;
         $SQL = 'SELECT id FROM cities ';
         $SQL .= 'WHERE name_clean = "' . $db->escape($city_name) .'" ';
         $SQL .= 'AND region_id = ' . $db->escape($reg_id);
         return $db->get_var($SQL);
      }
   }
?>